﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using JXT.PrimaryKey.Batman.Extensions;
using JXT.PrimaryKey.Batman.Validation;
using JXT.PrimaryKey.Batman.Collections;
using JXT.PrimaryKey.Batman.Domain.Models;
using JXT.PrimaryKey.Batman.Domain.Services;
using JXT.PrimaryKey.Batman.Domain.Specifications;
using JXT.PrimaryKey.Batman.Domain.UserSystem.Models;
using JXT.PrimaryKey.Batman.Domain.UserSystem.Repositories;

namespace JXT.PrimaryKey.Batman.Domain.UserSystem.Services.Default
{
    public class CallTaskService : ServiceBase<CallTask>, ICallTaskService
    {
        
        public void Add(CallTask entity)
        {
            if (entity == null)
                throw new ArgumentNullException("entity");
            Uri uri;
            if (!Uri.TryCreate(entity.Url, UriKind.Absolute, out uri))
            {
                throw new UriFormatException(String.Format("\"{0}\" is not a url!", entity.Url));
            }
            string summary = entity.Summary == null ? String.Empty : entity.Summary;
            Add(uri, summary);
        }

        public void Add(Uri url, string summary = "")
        {
            CallTask entity = new CallTask()
            {
                AddTime = DateTime.Now,
                IsFinish = false,
                FinishTime = DateTime.MinValue,
                Summary = summary,
                Url = url.ToString(),
                IsError = false,
                RetryCount = 0
            };
            LoadRepository<ICallTaskRepository>().Add(entity);
        }

        public IPagedCollection<CallTask> GetList(IPagedSpecification<CallTask> specification)
        {
            return LoadRepository<ICallTaskRepository>().GetList(specification);
        }
    }
}
